using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_LatestIssueTemplate : BaseTemplate
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FixTopicImage("~/img/issues.jpg");
        if (!IsPostBack)
        {
            //Find the id of the latest article
            foreach (Acms.Bll.Post post in this.CurrentPost.Children)
            {
                if (post.TemplateId.ToString() == ConfigurationManager.AppSettings["IssueTemplateId"])
                {
                    Master.SetTopicTitle(string.Concat(post.DisplayName));
                    latestPost = post;
                    latestId = post.Id;
                    latestIssueUrl = string.Format("{0}?SelectedTopic={{0}}", post.Url);
                    break;
                }
            }
        }
    }

    protected int GetTopicId(Acms.WebControls.AcmsDataListItem item)
    {
        int topicId = 0;
        if (dlTopics.DataKeys[item.ItemIndex] != null && dlTopics.DataKeys[item.ItemIndex].Value is int)
        {
            topicId = (int)dlTopics.DataKeys[item.ItemIndex].Value;
        }
        return topicId;
    }

    protected Acms.Bll.Post GetPost(Acms.WebControls.AcmsDataListItem item)
    {
        Acms.Bll.Post tmp = null;
        int topicId = GetTopicId(item);
        if (topicId > 0)
        {
            //limit to first
            int totalRecords = 0;
            System.Collections.Generic.List<Acms.Bll.Post> posts = Acms.Bll.Post.GetAllByParentIdAndTopicId(latestId, topicId, false, 0, 1, ref totalRecords);
            if (posts.Count > 0)
            {
                tmp = posts[0];
            }
        }
        return tmp;
    }

    protected void dlTopics_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsNormalItem)
        {
            Acms.Bll.Post post = GetPost(e.Item);
            if (post != null)
            {
                Acms.AcmsControls.AcmsImageLink imgThumb = e.Item.FindControl("imgThumb") as Acms.AcmsControls.AcmsImageLink;
                HyperLink lnkTitle = e.Item.FindControl("lnkTitle") as HyperLink;
                HtmlGenericControl pSummary = e.Item.FindControl("pSummary") as HtmlGenericControl;
                if (imgThumb != null)
                {
                    imgThumb.FileId = post.ThumbnailId;
                }
                if (lnkTitle != null)
                {
                    lnkTitle.Text = post.DisplayName;
                }
                if (pSummary != null)
                {
                    pSummary.InnerHtml = string.Concat(post.Summary, "...");
                }
            }
            else
            {
                e.Item.Visible = false;
            }
        }
    }

    private int latestId;
    private string latestIssueUrl;
    protected string LatestIssueUrl
    {
        get { return latestIssueUrl; }
    }
    private Acms.Bll.Post latestPost;

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Acms.Bll.Topic.GetAllBySiteId(CurrentPost.SiteId));
    }

    protected override object SaveViewState()
    {
        ViewState["LatestId"] = latestId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        if (ViewState["LatestId"] is int)
            latestId = (int)ViewState["LatestId"];
    }
}
